package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysGroupDuty;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysGroupDutyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysGroupDuty 控制层
 *
 * @author 15971
 * @date 2019/12/27 14:42:27
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "sys_group_duty", description = "sys_group_duty的API描述")
public class SysGroupDutyController extends SystemBaseController {

    @Resource
    private SysGroupDutyService sysGroupDutyService;

    @ApiOperation(value = "根据sid获取sys_group_duty", notes = "根据sid获取sys_group_duty")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "sys_group_duty", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysGroupDutyBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysGroupDutyBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, CommonParamNameConstants.SID);
            super.checkEmpty(sid, CommonMsgNameConstants.EMPTY_MSG_SID);
            SysGroupDuty sysGroupDuty = sysGroupDutyService.queryBySid(sid);
            hr.setData(sysGroupDuty);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据sids删除sys_group_duty", notes = "根据sids删除sys_group_duty")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "sys_group_dut的sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysGroupDuty.action", method = RequestMethod.POST)
    public HttpResult<Object> deleteSysGroupDuty() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get(CommonParamNameConstants.SIDS);
            super.checkEmpty(sids, CommonMsgNameConstants.EMPTY_MSG_SIDS);
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            sysGroupDutyService.batchDeleteDuty(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存sys_group_duty", notes = "保存sys_group_duty，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "sys_group_duty", required = true, paramType = "body", dataType = "SysGroupDuty")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysGroupDuty.action", method = RequestMethod.POST)
    public HttpResult<Object> saveSysGroupDuty() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysGroupDuty t = JSON.parseObject(json, SysGroupDuty.class);
            sysGroupDutyService.saveSysGroupDuty(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询sys_group_duty", notes = "分页查询sys_group_duty")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysGroupDutys.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysGroupDutys() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.GROUP_SID, true, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.NAME, false, false);
            PageBean page = new PageBean(map);
            List<Map<String, Object>> list = sysGroupDutyService.findSysGroupDuty(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询sys_group_duty", notes = "查询sys_group_duty")
    @ResponseBody
    @RequestMapping(value = "/findSysGroupDuty.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysGroupDuty() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.GROUP_SID, true, false);
            PageBean page = new PageBean(map);
            List<SysGroupDuty> list = sysGroupDutyService.findSysGroupDutys(params);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "批量删除部门", notes = "批量删除部门")
    @ResponseBody
    @RequestMapping(value = "/batchDeleteDuty.action", method = RequestMethod.POST)
    public HttpResult<Object> batchDeleteDuty() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            List<String> sids = super.getParamsList(map, CommonParamNameConstants.SIDS, String.class, true);
            sysGroupDutyService.batchDeleteDuty(sids, opUser);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
